package Utils;

import android.content.SharedPreferences;
import android.preference.PreferenceActivity;
import android.support.annotation.Nullable;

import net.ck.hzq.TobuyApplication;

import java.util.Map;
import java.util.Set;

/**
 * Created by mac on 2017/9/8.
 * SharedPreference封装
 */
public class SharedPrefStore implements SharedPreferences {
    private SharedPreferences sp;

    public SharedPrefStore() {
        sp = TobuyApplication.getmInstance().getSharedPreferences("ToBuy", PreferenceActivity.MODE_PRIVATE);
    }

    public SharedPreferences getSp() {
        return sp;
    }

    public boolean putString(String key, @Nullable String value) {
        return sp.edit().putString(key, value).commit();
    }

    public boolean putStringSet(String key, @Nullable Set<String> values) {
        return sp.edit().putStringSet(key, values).commit();
    }

    public SharedPrefStore putInt(String key, int value) {
        sp.edit().putInt(key, value).apply();
        return this;
    }

    public SharedPrefStore putLong(String key, long value) {
        sp.edit().putLong(key, value).apply();
        return this;
    }

    public SharedPrefStore putFloat(String key, float value) {
        sp.edit().putFloat(key, value).apply();
        return this;
    }

    public SharedPrefStore putBoolean(String key, boolean value) {
        sp.edit().putBoolean(key, value).apply();
        return this;
    }

    public SharedPrefStore remove(String key) {
        sp.edit().remove(key).apply();
        return this;
    }

    public boolean clear() {
        return sp.edit().clear().commit();
    }

    @Override
    public Map<String, ?> getAll() {
        return sp.getAll();
    }

    @Override
    @Nullable
    public String getString(String key, @Nullable String defValue) {
        return sp.getString(key, defValue);
    }

    @Override
    @Nullable
    public Set<String> getStringSet(String key, @Nullable Set<String> defValues) {
        return sp.getStringSet(key, defValues);
    }

    @Override
    public int getInt(String key, int defValue) {
        return sp.getInt(key, defValue);
    }

    @Override
    public long getLong(String key, long defValue) {
        return sp.getLong(key, defValue);
    }

    @Override
    public float getFloat(String key, float defValue) {
        return sp.getFloat(key, defValue);
    }

    @Override
    public boolean getBoolean(String key, boolean defValue) {
        return sp.getBoolean(key, defValue);
    }

    @Override
    public boolean contains(String key) {
        return sp.contains(key);
    }

    @Override
    public Editor edit() {
        return sp.edit();
    }

    @Override
    public void registerOnSharedPreferenceChangeListener(OnSharedPreferenceChangeListener listener) {
        sp.registerOnSharedPreferenceChangeListener(listener);
    }

    @Override
    public void unregisterOnSharedPreferenceChangeListener(OnSharedPreferenceChangeListener listener) {
        sp.unregisterOnSharedPreferenceChangeListener(listener);
    }

}
